package com.neu.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.api.dto.CourseDTO;
import com.neu.api.mapper.CourseMapper;
import com.neu.api.pojo.Course;
import com.neu.api.query.CourseQuery;
import com.neu.api.service.CourseService;
import com.neu.api.utils.PageResult;
import com.neu.api.vo.CourseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class CourseServiceImpl implements CourseService {

//    @Value("${neu.password}")
//    private String password;

    @Resource
    CourseMapper courseMapper;

    @Override
    public void add(CourseDTO courseDTO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO,course);
        course.setId(2);
        System.out.println("加入课程中");
        courseMapper.add(course);

        //        User user = new User();  //name loginName phone
//        BeanUtils.copyProperties(userDTO,user);
//
//        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
//        user.setCreateTime(new Date());
//        user.setUpdateTime(new Date());
//
//        user.setStatus(StatusConst.ACTIVE_STATUS);
//
//        userMapper.add(user);
    }

    @Override
    public void deleteById(int id) {
//        deleteById
        courseMapper.deleteById(id);

    }


    @Override
    public void update(int id, CourseDTO courseDTO) {
////                                           //name loginName phone
////        //dto -- > pojo
////        User user = new User();
////        BeanUtils.copyProperties(userDTO,user);
////
////        user.setId(id);
////        user.setUpdateTime(new Date());
////        userMapper.update(user);
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO,course);
        course.setId(id);
        courseMapper.update(course);
    }

    @Override
    public CourseVO findById(int id) {

        return courseMapper.findById(id);
    }

    @Override
    public PageResult<CourseVO> find(CourseQuery courseQuery) {
        PageHelper.startPage(courseQuery.getPage(), courseQuery.getPageSize());

        Page<CourseVO> page = (Page<CourseVO>) courseMapper.find(courseQuery);

        PageInfo<CourseVO> pageInfo = page.toPageInfo();


        List<CourseVO> courseVOList = pageInfo.getList();
        long total = pageInfo.getTotal();

        PageResult<CourseVO> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setList(courseVOList);

        return pageResult;


        ////        PageHelper.startPage(userQuery.getPage(),userQuery.getPageSize());
        ////
        ////        Page<UserVO> page = (Page<UserVO>) userMapper.find(userQuery);
        ////
        ////        PageInfo<UserVO> pageInfo = page.toPageInfo();
        ////
        ////
        ////        List<UserVO> userVOList = pageInfo.getList();
        ////        long total = pageInfo.getTotal();
        ////
        ////        PageResult<UserVO> pageResult = new PageResult<>();
        ////        pageResult.setTotal(total);
        ////        pageResult.setList(userVOList);
        ////
        ////        return pageResult;
    }

//
//    @Override
//    public PageResult<UserVO> find(UserQuery userQuery) {
//
//        PageHelper.startPage(userQuery.getPage(),userQuery.getPageSize());
//
//        Page<UserVO> page = (Page<UserVO>) userMapper.find(userQuery);
//
//        PageInfo<UserVO> pageInfo = page.toPageInfo();
//
//
//        List<UserVO> userVOList = pageInfo.getList();
//        long total = pageInfo.getTotal();
//
//        PageResult<UserVO> pageResult = new PageResult<>();
//        pageResult.setTotal(total);
//        pageResult.setList(userVOList);
//
//        return pageResult;
//    }
//
//    @Override
//    public void add(UserDTO userDTO) {
//        User user = new User();  //name loginName phone
//        BeanUtils.copyProperties(userDTO,user);
//
//        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
//        user.setCreateTime(new Date());
//        user.setUpdateTime(new Date());
//
//        user.setStatus(StatusConst.ACTIVE_STATUS);
//
//        userMapper.add(user);
//    }
//
//    @Override
//    public UserVO login(String loginName, String password) {
//        password = DigestUtils.md5DigestAsHex(password.getBytes());
//        return userMapper.login(loginName,password);
//    }
//
//    @Override
//    public void update(long id, UserDTO userDTO) {
//                                           //name loginName phone
//        //dto -- > pojo
//        User user = new User();
//        BeanUtils.copyProperties(userDTO,user);
//
//        user.setId(id);
//        user.setUpdateTime(new Date());
//        userMapper.update(user);
//    }
//
//    @Override
//    public void deleteById(long id) {
//        userMapper.deleteById(id);
//    }
//
//    @Override
//    public void updateStatus(long id, byte status) {
//        User user = new User();
//        user.setId(id);
//        user.setUpdateTime(new Date());
//        user.setStatus(status);
//        userMapper.update(user);
//    }
}
